# Appendix G
# Santiago Lopez-Cariboni, 2022. "Political Regimes and Informal Social Insurance", Comparative Political Studies.


# ---- wd ----
wd <- 'replication_materials' 
setwd(wd)

# ---- packages ----
pkgs <- c("data.table", "foreign", 'panelView', 'fect', "lubridate", 
    "ggplot2", "dplyr", "mFilter", "texreg", "AER", "xts", "interflex", "gridExtra")
suppressPackageStartupMessages(sapply(pkgs,require,character.only=T))
# Plot the effects (use Johannes Karreth's function: https://github.com/jkarreth/JKmisc/blob/master/ggintfun.R)
# devtools::source_url("https://raw.githubusercontent.com/jkarreth/JKmisc/master/ggintfun.R")
source('code/functions/ggintfun.R')
# Function for time Lags
source('code/functions/lagpanel.R')


# ---- data ----
dt <- fread('data/dt_replication.csv')
dt <- dt[LDC==1, ]


# ---- Table 4 ----
source('../code/lagpanel.R')
dt <- dt[order(dt$iso3c, dt$year),] # sort or lagpanel
dt$l.r_prv <- lagpanel(dt$r_prv, dt$iso3c, dt$year, 1) 

m <- lm(outgap.tdl ~ l.outgap.tdl 
    + l.outgap.gdp.hamilton * l.democracy
    + l.r_prv
    - l.r_prv
    + as.factor(iso3c)
    + as.factor(year)
     ,
    data=dt, na.action=na.omit)
summary(m,  diagnostics = TRUE)
nc <- length(unique(m$model[['as.factor(iso3c)']]))

m.priv <- lm(outgap.tdl ~ l.outgap.tdl 
    + l.outgap.gdp.hamilton * l.democracy
    + l.r_prv
    + as.factor(iso3c)
    + as.factor(year)
     ,
    data=dt, na.action=na.omit)
summary(m.priv,  diagnostics = TRUE)
nc.priv <- length(unique(m.priv$model[['as.factor(iso3c)']]))


m.priv.controls <- lm(outgap.tdl ~ l.outgap.tdl 
    + l.outgap.gdp.hamilton * l.democracy
    + l.r_prv
    + l.capacity
    + l.imports
    + l.exports
    + log(l.pop)
    + log(l.gdp.pcap)
    + l.elec.cons.pc
    + l.pop.density
    + as.factor(iso3c)
    + as.factor(year)
     ,
    data=dt, na.action=na.omit)
summary(m.priv.controls,  diagnostics = TRUE)
nc.priv.controls <- length(unique(m.priv.controls$model[['as.factor(iso3c)']]))

v.names <- c(
    "Losses Cycle$_{t-1}$",
    "GDP Output Gap$_{t-1}$",
    "Democracy$_{t-1}$",
    "Democracy$_{t-1}$ $\\times$ GDP Output Gap$_{t-1}$",
    "Privatization$_{t-1}$",
    "State Capacity$_{t-1}$",
    "Imports$_{t-1}$",
    "Exports$_{t-1}$",
    "Population (log)$_{t-1}$",
    "Real GDP per capita (log)$_{t-1}$",
    "Electricity Consumption$_{t-1}$",
    "Population Density$_{t-1}$"
    )


table1 <- texreg(list(m, m.priv, m.priv.controls),
    file= "tables/OA/T4_privat.tex",
    label="tab:private",
    custom.model.names = c('(1)','(2)', '(3)'),
    caption="Ciclicality of Electricity Losses in Autocracies and Democracies controlling for Privatization",
    dcolumn = TRUE,
    no.margin=FALSE,
    fontsize="scriptsize",
    single.row=FALSE,
    use.packages=FALSE,
    booktabs = TRUE,
    digits=2,
    float.pos="htbp",
    sideways=FALSE,
    omit.coef= "(year)|(iso3c)|(Intercept)",
    include.rsquared = FALSE,
    stars = c(0.01, 0.05, 0.1),
    custom.gof.rows = list("Year dummies" = c("Yes", "Yes", "Yes"), 
        "Country fixed-effects" = c("Yes", "Yes", "Yes"),
        "Number of countries"= c(nc, nc.priv, nc.priv.controls)),
    # reorder.gof = c(1, 2, 5, 3, 4),
    custom.coef.names=v.names
    )

# ---- Figure G.1 ----
ml.r_prv_auto <- lm(outgap.tdl ~ l.outgap.tdl 
    + l.outgap.gdp.hamilton * r_prv 
    + l.imports
    + l.exports
    + log(l.pop)
    + log(l.gdp.pcap)
    + l.elec.cons.pc
    + l.pop.density
    + as.factor(iso3c)
    + as.factor(year)
     ,
    data=dt[democracy==0,], na.action=na.omit)
summary(ml.r_prv_auto,  diagnostics = TRUE)
ncl.r_prv_auto <- length(unique(ml.r_prv_auto$model[['as.factor(iso3c)']]))

pdf("figures/OA/G1_priv_auto.pdf", width = 8, height = 6)
ggintfun(obj = ml.r_prv_auto, varnames = c("l.outgap.gdp.hamilton", "r_prv"), 
         varlabs = c("GDP Output Gap", "Corporatization of State-Owned Utilities"),
         title = FALSE, rug = TRUE, rugsize = 0.2, jitter_factor = 3,
         twoways = FALSE) +
coord_cartesian(ylim = c(-.15, .1), xlim =c(1, 2)) +
ggtitle("Autocracies")
dev.off()


ml.r_prv <- lm(outgap.tdl ~ l.outgap.tdl 
    + l.outgap.gdp.hamilton * r_prv 
    + l.imports
    + l.exports
    + log(l.pop)
    + log(l.gdp.pcap)
    + l.elec.cons.pc
    + l.pop.density
    + as.factor(iso3c)
    + as.factor(year)
     ,
    data=dt[democracy==1,], na.action=na.omit)
summary(ml.r_prv,  diagnostics = TRUE)
ncl.r_prv <- length(unique(ml.r_prv$model[['as.factor(iso3c)']]))

pdf("figures/OA/G1_priv_demo.pdf", width = 8, height = 6)
ggintfun(obj = ml.r_prv, varnames = c("l.outgap.gdp.hamilton", "r_prv"), 
         varlabs = c("GDP Output Gap", "Corporatization of State-Owned Utilities"),
         title = FALSE, rug = TRUE, rugsize = 0.2, jitter_factor = 3,
         twoways = FALSE) +
coord_cartesian(ylim = c(-.15, .1), xlim =c(1, 2)) +
ggtitle("Democracies")
dev.off()




